Kotlin
Kotlin Collections Overview
Swift
|
Collection types
|
val numbers = mutableListOf("one", "two", "three", "four")
numbers.add("five") // this is OK
//numbers = mutableListOf("six", "seven") // compilation error
|
let numbers = mutableListOf("one", "two", "three", "four")
numbers.add("five") // this is OK
//numbers = mutableListOf("six", "seven") // compilation error
|
Collection
|
fun printAll(strings: Collection<String>) {
for(s in strings) print("$s ")
println()
}
fun main() {
val stringList = listOf("one", "two", "one")
printAll(stringList)
val stringSet = setOf("one", "two", "three")
printAll(stringSet)
}
|
func printAll(strings: Collection<String>) {
for(s in strings) print("$s ")
print()
}
func main() {
let stringList = listOf("one", "two", "one")
printAll(stringList)
let stringSet = setOf("one", "two", "three")
printAll(stringSet)
}
|
fun List<String>.getShortWordsTo(shortWords: MutableList<String>, maxLength: Int) {
this.filterTo(shortWords) { it.length <= maxLength }
// throwing away the articles
val articles = setOf("a", "A", "an", "An", "the", "The")
shortWords -= articles
}
fun main() {
val words = "A long time ago in a galaxy far far away".split(" ")
val shortWords = mutableListOf<String>()
words.getShortWordsTo(shortWords, 3)
println(shortWords)
}
|
func List<String>.getShortWordsTo(shortWords: MutableList<String>, maxLength: Int) {
this.filterTo(shortWords) { it.length <= maxLength }
// throwing away the articles
let articles = setOf("a", "A", "an", "An", "the", "The")
shortWords -= articles
}
func main() {
let words = "A long time ago in a galaxy far far away".split(" ")
let shortWords = mutableListOf<String>()
words.getShortWordsTo(shortWords, 3)
print(shortWords)
}
|
List
|
val numbers = listOf("one", "two", "three", "four")
println("Number of elements: ${numbers.size}")
println("Third element: ${numbers.get(2)}")
println("Fourth element: ${numbers[3]}")
println("Index of element \"two\" ${numbers.indexOf("two")}")
|
let numbers = listOf("one", "two", "three", "four")
print("Number of elements: ${numbers.size}")
print("Third element: ${numbers.get(2)}")
print("Fourth element: ${numbers[3]}")
print("Index of element \"two\" ${numbers.indexOf("two")}")
|
val bob = Person("Bob", 31)
val people = listOf(Person("Adam", 20), bob, bob)
val people2 = listOf(Person("Adam", 20), Person("Bob", 31), bob)
println(people == people2)
bob.age = 32
println(people == people2)
|
let bob = Person("Bob", 31)
let people = listOf(Person("Adam", 20), bob, bob)
let people2 = listOf(Person("Adam", 20), Person("Bob", 31), bob)
print(people == people2)
bob.age = 32
print(people == people2)
|
val numbers = mutableListOf(1, 2, 3, 4)
numbers.add(5)
numbers.removeAt(1)
numbers[0] = 0
numbers.shuffle()
println(numbers)
|
let numbers = mutable[1, 2, 3, 4]
numbers.add(5)
numbers.removeAt(1)
numbers[0] = 0
numbers.shuffle()
print(numbers)
|
Set
|
val numbers = setOf(1, 2, 3, 4)
println("Number of elements: ${numbers.size}")
if (numbers.contains(1)) println("1 is in the set")
val numbersBackwards = setOf(4, 3, 2, 1)
println("The sets are equal: ${numbers == numbersBackwards}")
|
let numbers = setOf(1, 2, 3, 4)
print("Number of elements: ${numbers.size}")
if (numbers.contains(1)) print("1 is in the set")
let numbersBackwards = setOf(4, 3, 2, 1)
print("The sets are equal: ${numbers == numbersBackwards}")
|
val numbers = setOf(1, 2, 3, 4) // LinkedHashSet is the default implementation
val numbersBackwards = setOf(4, 3, 2, 1)
println(numbers.first() == numbersBackwards.first())
println(numbers.first() == numbersBackwards.last())
|
let numbers = setOf(1, 2, 3, 4) // LinkedHashSet is the default implementation
let numbersBackwards = setOf(4, 3, 2, 1)
print(numbers.first() == numbersBackwards.first())
print(numbers.first() == numbersBackwards.last())
|
Map
|
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
println("All keys: ${numbersMap.keys}")
println("All values: ${numbersMap.values}")
if ("key2" in numbersMap) println("Value by key \"key2\": ${numbersMap["key2"]}")
if (1 in numbersMap.values) println("The value 1 is in the map")
if (numbersMap.containsValue(1)) println("The value 1 is in the map") // same as previous
|
let numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
print("All keys: ${numbersMap.keys}")
print("All values: ${numbersMap.values}")
if ("key2" in numbersMap) print("Value by key \"key2\": ${numbersMap["key2"]}")
if (1 in numbersMap.values) print("The value 1 is in the map")
if (numbersMap.containsValue(1)) print("The value 1 is in the map") // same as previous
|
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
val anotherMap = mapOf("key2" to 2, "key1" to 1, "key4" to 1, "key3" to 3)
println("The maps are equal: ${numbersMap == anotherMap}")
|
let numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
let anotherMap = mapOf("key2" to 2, "key1" to 1, "key4" to 1, "key3" to 3)
print("The maps are equal: ${numbersMap == anotherMap}")
|
val numbersMap = mutableMapOf("one" to 1, "two" to 2)
numbersMap.put("three", 3)
numbersMap["one"] = 11
println(numbersMap)
|
let numbersMap = mutableMapOf("one" to 1, "two" to 2)
numbersMap.put("three", 3)
numbersMap["one"] = 11
print(numbersMap)
|